package com.luoye.lj.wstool.wsdm;

import com.luoye.lj.wstool.generator.ICodeGenerator;
import com.luoye.lj.wstool.wsdm.entity.WsdtEntity;
import com.luoye.lj.wstool.wsdm.entity.WsdtField;

/*
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 2016/4/26
 * Time: 15:43
 * To change this template use File | Settings | File Templates.
 */
public class PrimaryParamCodeGenerator implements ICodeGenerator {
    private WsdtEntity wsdtEntity;
    private boolean needParamA=true;
    private boolean needType=true;

    public PrimaryParamCodeGenerator(WsdtEntity wsdtEntity) {
        this.wsdtEntity = wsdtEntity;
    }

    public PrimaryParamCodeGenerator(WsdtEntity wsdtEntity, boolean needParamA) {
        this.wsdtEntity = wsdtEntity;
        this.needParamA = needParamA;
    }

    public PrimaryParamCodeGenerator(WsdtEntity wsdtEntity, boolean needParamA, boolean needType) {
        this.wsdtEntity = wsdtEntity;
        this.needParamA = needParamA;
        this.needType = needType;
    }

    @Override
    public String codeGenerate() {
        String code="";
        WsdtField field=null;
        if(this.wsdtEntity!=null){
            if(this.wsdtEntity.getFieldList()!=null){
                for(WsdtField wsdtField:this.wsdtEntity.getFieldList()){
                    if(wsdtField.isPrimary()) {
                        if(needParamA) {
                            code += "@Param(\"" + wsdtField.getFieldName() + "\")" + wsdtField.getFieldType() + " " + wsdtField.getFieldName() + ",";
                        }else{
                            if(needType) {
                                code += "" + wsdtField.getFieldType() + " " + wsdtField.getFieldName() + ",";
                            }else {
                                code += wsdtField.getFieldName() + ",";
                            }
                        }
                    }
                    field=wsdtField;
                }
            }
        }
        if("".equals(code)){
            if(field!=null){
                if(needParamA) {
                    code += "@Param(\"" + field.getFieldName() + "\")" + field.getFieldType() + " " + field.getFieldName();
                }else{
                    if(needType) {
                        code += "" + field.getFieldType() + " " + field.getFieldName();
                    }else {
                        code += field.getFieldName();
                    }
                }
            }
        }else {
            code=code.substring(0,code.length()-1);
        }
        return code;
    }
}
